package com.cskaoyan.gateway.controller.shopping;

import com.cskaoyan.gateway.form.shopping.ProductForm;
import com.cskaoyan.gateway.form.shopping.ProductVOForm;
import com.mall.shopping.dto.SearchProductRequest;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.IContentService;
import com.mall.shopping.IHomeService;
import com.mall.shopping.IProductCateService;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

/**
 * @author liyongqiang
 * CreateDate 2021/7/21 11:41
 */

@RestController
@RequestMapping("/shopping")
@Anoymous
public class ShoppingController {

    @Reference(timeout = 3000, check = false)
    IProductService productService;

    @GetMapping("/product/{id}")
    public ResponseData getProductDetail(@PathVariable("id") Long productId){
        ProductDetailRequest productDetailRequest = new ProductDetailRequest();
        productDetailRequest.setId(productId);

        ProductDetailResponse productDetail = productService.getProductDetail(productDetailRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(productDetail.getCode())){
            //调用成功
            return new ResponseUtil().setData(productDetail.getProductDetailDto());
        }
        //调用失败
        return new ResponseUtil().setErrorMsg(productDetail.getMsg());


    }

    @GetMapping("/goods")
    public ResponseData getProductList(ProductForm productForm){
        System.out.println(11);
        AllProductRequest allProductRequest = new AllProductRequest();
        allProductRequest.setPage(productForm.getPage());
        allProductRequest.setSize(productForm.getSize());
        allProductRequest.setSort(productForm.getSort());
        allProductRequest.setPriceGt(productForm.getPriceGt());
        allProductRequest.setPriceLte(productForm.getPriceLte());
        if (productForm.getCid() != null) {
            allProductRequest.setCid(productForm.getCid().longValue());
        }
        AllProductResponse allProduct = productService.getAllProduct(allProductRequest);
        ProductVOForm productVOForm = new ProductVOForm();
        productVOForm.setData(allProduct.getProductDtoList());
        productVOForm.setTotal(allProduct.getTotal());
        if (ShoppingRetCode.SUCCESS.getCode().equals(allProduct.getCode())){
            return new ResponseUtil().setData(productVOForm);
        }
        return new ResponseUtil().setErrorMsg(allProduct.getMsg());

    }

    @GetMapping("/recommend")
    public ResponseData getRecommendGoods(){
        RecommendResponse recommendResponse = productService.getRecommendGoods();
        if (ShoppingRetCode.SUCCESS.getCode().equals(recommendResponse.getCode())){
            //调用成功
            return new ResponseUtil<>().setData(recommendResponse.getPanelContentItemDtos());
        }
        //调用失败
        return new ResponseUtil<>().setErrorMsg(recommendResponse.getMsg());
    }


    /*---------------------------------shuiYi--------------------*/

    @Reference(interfaceClass = IContentService.class, timeout = 3000, retries = 0, check = false)
    IContentService contentService;

    @Reference(interfaceClass = IProductCateService.class, timeout = 10000, retries = 0, check = false)
    IProductCateService productCateService;

    @Reference(interfaceClass = IHomeService.class, timeout = 3000, retries = 0, check = false)
    IHomeService iHomeService;

    @Anoymous
    @RequestMapping("homepage")
    public ResponseData shoppingHomepage () {
        HomePageResquest homePageResquest = new HomePageResquest();

        HomePageResponse homePageResponse = iHomeService.homepage(homePageResquest);
        if (ShoppingRetCode.SHOPPING_HOMEPAGE.getCode().equals(homePageResponse.getCode())) {
            //调用成功
            return new ResponseUtil().setData(homePageResponse.getPanelContentItemDtos());
        }
        //请求失败
        return new ResponseUtil().setErrorMsg(homePageResponse.getMsg());
    }

    @Anoymous
    @GetMapping("navigation")
    public ResponseData shoppingNavigation () {
        NavListResponse navListResponse = contentService.queryNavList();

        if (ShoppingRetCode.SHOPPING_HOMEPAGE.getCode().equals(navListResponse.getCode())) {
            return new ResponseUtil().setData(navListResponse.getPannelContentDtos());
        }
        return new ResponseUtil().setErrorMsg(navListResponse.getMsg());
    }

    @Anoymous
    @GetMapping("categories")
    public ResponseData shoppingCategories (String sort){
        AllProductCateRequest allProductCateRequest = new AllProductCateRequest();
        allProductCateRequest.setSort(sort);
        AllProductCateResponse allProductCate = productCateService.getAllProductCate(allProductCateRequest);
        if (ShoppingRetCode.SHOPPING_HOMEPAGE.getCode().equals(allProductCate.getCode())) {

            return new ResponseUtil().setData(allProductCate.getProductCateDtoList());
        }

        return new ResponseUtil().setErrorMsg(allProductCate.getMsg());

    }

    @Anoymous
    @GetMapping("search")
    public ResponseData searchProduct(SearchProductRequest searchProductRequest) {
        // TODO 商品搜索未做
        // SearchProductResponse searchProductResponse = productService.searchProduct(searchProductRequest);
        return new ResponseData();
    }
}
